<template>
  <div class="Gender-container">
    <van-picker
       title="性别"
       show-toolbar
       :columns="columns"
       @confirm="onConfirm"
       @cancel="$emit('close')"
       @change="onPickerChange"
       :default-index="value"
    />
  </div>
</template>

<script>
import { updataUserProfile } from '@/api/user'
export default {
  name: 'UpdateGender',
  props: {
    value: {
      type: Number,
      require: true
    }
  },
  data () {
    return {
      columns: ['男', '女'],
      localGender: this.value
    }
  },

  mounted () {

  },

  methods: {
    async onConfirm () {
      try {
        this.$toast.loading({
          message: '保存中...',
          forbidClick: true, // 禁用背景点击
          duration: 0 // 如果为0，则持续展示

        })
        const localGender = this.localGender

        await updataUserProfile({
          gender: localGender
        })
        // 更新视图
        this.$emit('input', localGender)
        this.$emit('close')
        this.$toast.success('更新成功')
      } catch (err) {
        this.$toast.fail('更新失败')
      }
    },
    onPickerChange (picker, value, index) {
      this.localGender = index
    }
  }
}
</script>

<style lang="less" scoped>

</style>
